/*********************************************************
@file

@author J. Di Mattina

@brief test cases for the c-realize library serialization.

Tests the implentation of the c-realize serialization 
library.
*********************************************************/
/* standard library includes */
#include <assert.h>
#include <stdint.h>
#include <stdio.h>

/* local includes */
#include "Crz.h"

/*********************************************************
 primitiveTest
*//**
@brief Pack primitives into a buffer

@return None
**********************************************************/
static void
primitiveTest()
{
    Crz_Result result;
    char *desc = "124";
    char *buf = NULL;

    /* dummy data */
    uint8_t u8 = 0x42;
    uint16_t u16 = 0x1776;
    uint32_t u32 = 0xcafebabe;

    result = Crz_Serialize(&buf, desc, u8, u16, u32);

    if (result == CRZ_SUCCESS)
    {
#ifdef DEBUG
        int i;
        /* print out the bytes */
        for (i = 0; i < 7; i++)
        {
            printf("Byte[%d]: %02x\n", i, buf[i] & 0xFF /*eliminate sign extension */);
        }
#endif
        /* spot check */
        assert(buf[1] == 0x17);
        /* keeps sign extending my bytes... this is what 
           happens when you declare char instead of
           unsigned char... */
        assert((buf[6] & 0xff) == 0xbe);
        printf("    Primitive Test: PASSED\n");
    }
    else
    {
        printf("    Primitive Test: FAILED\n");
    }

}

/*********************************************************
 arrayTest
*//**
@brief Pack arrays into a buffer

@return None
**********************************************************/
static void
arrayTest()
{
    Crz_Result result;
    char *desc = "*2";
    char *buf = NULL;

    /* dummy data */
    uint16_t testArray[10];

    int i;

    /* fill dummy array */
    for(i = 0; i < 10; i++)
    {
        testArray[i] = i * 16;
    }

    result = Crz_Serialize(&buf, desc, testArray, 10);

    if (result == CRZ_SUCCESS)
    {
#ifdef DEBUG
        uint16_t *buf16 = buf;

        /* print out intial bytes */
        for (i = 0; i < 10; i++)
        {
            printf("TestElement[%d]: %04x\n", i, testArray[i] & 0xFFFF);
        }

        /* print out serialized bytes */
        for (i = 0; i < 10; i++)
        {
            printf("BufferElement[%d]: %04x\n", i, buf16[i] & 0xFFFF);
        }
#endif
        /** spot check */
        assert((buf[3] & 0xFF) == 0x10);
        assert((buf[5] & 0xFF) == 0x20);
        assert((buf[7] & 0xFF) == 0x30);

        printf("    Array Test: PASSED\n");
    }
    else
    {
        printf("    Array Test: FAILED\n");
    }
}

/*********************************************************
 main
*//**
@brief main program function

@return 0 on success
        errno otherwise.
**********************************************************/
int main()
{
    printf("Starting Test Crz Serialize\n");
    primitiveTest();
    arrayTest();
    
    printf("Test Crz Serialize: PASSED\n");
    return 0;
}

